/***********************************************************************************
*              Copyright 2007 - 2010, Megaeyes. Co., Ltd.
*                           ALL RIGHTS RESERVED
* FileName: hi_server.c
* Description: main model .
*
* History:
* Version   Date              Author        DefectNum    Description
* 1.1       2007-08-22   q60002125     NULL            Create this file.
***********************************************************************************/
#ifdef __cplusplus
#if __cplusplus
extern "C"{
#endif
#endif /* __cplusplus */

#include "socket.h"
#include "thread.h"

char g_ip[] = "255.255.255.255";
int g_port = 0;

int user_socket = 0;
int  sys_socket = 0;
int  grp_socket = 0;

int Sock_Init()
{
    sys_socket  = CreateSocket(g_ip, 8000, 0);
	grp_socket  = CreateSocket(g_ip, 10000, 0);
    user_socket = CreateSocket(g_ip, g_port, 0);
}

int CreateSocket(const char* ip, int port, int zu)
{
	struct sockaddr_in addr = {0};

	//2.设置UDP的地址并绑定
    memset(&addr, 0, sizeof(struct sockaddr));
    addr.sin_family = AF_INET;                 //使用IPv4协议
    addr.sin_port = htons(port);               //网络通信都使用大端格式
    addr.sin_addr.s_addr = inet_addr(ip);      //绑定本地IP

	int sock = socket(AF_INET, SOCK_DGRAM, 0);

	int on = 1; //开启
    int ret = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
    if (ret < 0)
    {
        perror("setsockopt fail\n");
        return 0;
    }

    ret = bind(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr));
    if (ret < 0)
    {
        printf("port = %d ", port);
        perror("bind fail:");
        close(sock);
        return -1;
    }

	return sock;
}


#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */




